using FutureCoreDesign;
using UnityEditor;
using UnityEngine;

[CanEditMultipleObjects]
[CustomEditor(typeof(DesignCheckAnimator))]
public class EditorCheckAnimator_Inspector : Editor
{
    /// <summary>
    /// 滑动杆的当前时间
    /// </summary>
    private float m_CurTime;

    /// <summary>
    /// 是否已经烘培过
    /// </summary>
    private bool m_HasBake;

    /// <summary>
    /// 当前是否是预览播放状态
    /// </summary>
    private bool m_Playing;

    /// <summary>
    /// 当前运行时间
    /// </summary>
    private float m_RunningTime;

    /// <summary>
    /// 上一次系统时间
    /// </summary>
    private double m_PreviousTime;

    /// <summary>
    /// 总的记录时间
    /// </summary>
    private float m_RecorderStopTime;

    /// <summary>
    /// 滑动杆总长度
    /// </summary>
    private const float kDuration = 30f;

    private DesignCheckAnimator designCheckAnimator { get { return target as DesignCheckAnimator; } }

    private Animator m_Animator;

    private Animator animator
    {
        get { return m_Animator ?? (m_Animator = designCheckAnimator.GetComponentsInChildren<Animator>()[0]); }
    }

    void OnEnable()
    {
        m_PreviousTime = EditorApplication.timeSinceStartup;
        EditorApplication.update += InspectorUpdate;
    }

    void OnDisable()
    {
        EditorApplication.update -= InspectorUpdate;
    }

    public override void OnInspectorGUI()
    {
        //DrawDefaultInspector();

        EditorGUILayout.Space();
        EditorGUILayout.BeginHorizontal();
        if (GUILayout.Button("播放"))
        {
            Play();
        }
        if (GUILayout.Button("暂停"))
        {
            Stop();
        }
        //if (GUILayout.Button("烘培"))
        //{
        //    m_HasBake = false;
        //    Bake();
        //}

        EditorGUILayout.EndHorizontal();
        EditorGUILayout.Space();
        m_CurTime = EditorGUILayout.Slider("时间", m_CurTime, 0f, kDuration);
        ManualUpdate();
    }

    /// <summary>
    /// 烘培记录动画数据
    /// </summary>
    private void Bake()
    {
        if (m_HasBake)
        {
            return;
        }

        if (Application.isPlaying || animator == null)
        {
            return;
        }

        const float frameRate = 30f;
        const int frameCount = (int)((kDuration * frameRate) + 2);
        animator.Rebind();
        animator.StopPlayback();
        animator.recorderStartTime = 0;

        // 开始记录指定的帧数
        animator.StartRecording(frameCount);

        for (var i = 0; i < frameCount - 1; i++)
        {
            // 这里可以在指定的时间触发新的动画状态
            if (i == 200)
            {
                //animator.SetTrigger("XxState");
            }
            // 记录每一帧
            animator.Update(1.0f / frameRate);
        }
        // 完成记录
        animator.StopRecording();

        // 开启回放模式
        animator.StartPlayback();
        m_HasBake = true;
        m_RecorderStopTime = animator.recorderStopTime;
    }

    /// <summary>
    /// 进行预览播放
    /// </summary>
    private void Play()
    {
        if (Application.isPlaying || animator == null)
        {
            return;
        }

        Bake();
        m_RunningTime = 0f;
        m_Playing = true;
    }

    /// <summary>
    /// 停止预览播放
    /// </summary>
    private void Stop()
    {
        if (Application.isPlaying || animator == null)
        {
            return;
        }

        m_Playing = false;
        m_CurTime = 0f;
    }

    /// <summary>
    /// 预览播放状态下的更新
    /// </summary>
    private void Update()
    {
        if (Application.isPlaying || animator == null)
        {
            return;
        }

        if (m_RunningTime > m_RecorderStopTime)
        {
            m_Playing = false;
            return;
        }

        // 设置回放的时间位置
        animator.playbackTime = m_RunningTime;
        animator.Update(0);
        m_CurTime = m_RunningTime;
    }

    /// <summary>
    /// 非预览播放状态下，通过滑杆来播放当前动画帧
    /// </summary>
    private void ManualUpdate()
    {
        if (animator && !m_Playing && m_HasBake && m_CurTime < m_RecorderStopTime)
        {
            animator.playbackTime = m_CurTime;
            animator.Update(0);
        }
    }

    private void InspectorUpdate()
    {
        var delta = EditorApplication.timeSinceStartup - m_PreviousTime;
        m_PreviousTime = EditorApplication.timeSinceStartup;

        if (!Application.isPlaying && m_Playing)
        {
            m_RunningTime = Mathf.Clamp(m_RunningTime + (float)delta, 0f, kDuration);
            Update();
        }
    }
}